// index.js
import { getClassfiy, swiper, courseList ,Topic } from "../../api/index"
Page({
  data: {
    swiperList: [],
    courseList: [],
    TopicList:[]
  },
  goSearch() {
    wx.navigateTo({ url: '/pages/search/search' });
  },

  onLoad: async function(option) {
    try {
      // 先请求分类数据
      const classifyRes = await getClassfiy()
      console.log("分类数据：", classifyRes)
      
      // 同时请求精品课程和轮播图数据
      await Promise.all([this.fetchCourseList(), this.fetchSwiper(),this.Specialtopiclist()])
    } catch (error) {
      console.error("接口调用失败：", error)
    }
  },
  goIntroduct(data){
    let id = data.currentTarget.dataset.id;
    console.log(data);
   wx.navigateTo({
     url: `/pages/introduct/introduct?courseId=${id}`,
   })
  },
  goToMore(data) {
      let type = data.currentTarget.dataset.type;
      console.log(type);
    wx.navigateTo({
        url: `/pages/courseType/courseType?type=${type}`,
      })
  },
//专题列表请求
async Specialtopiclist() {
  try {
    const res = await Topic() 
    console.log("专题列表数据:", res)
    this.setData({
      TopicList: res.rows || [],
    })
  } catch (error) {
    console.error("专题接口调用失败：", error)
  }
},
  // 精品课程请求函数
  async fetchCourseList() {
    try {
      const res = await courseList() 
      console.log("课程列表数据:", res)
      this.setData({
        courseList: res.rows || []
      })
    } catch (error) {
      console.error("精品课程接口调用失败：", error)
    }
  },

  // 轮播图请求函数
  async fetchSwiper() {
    try {
      const res = await swiper() 
      console.log("轮播图数据:", res)
      this.setData({
        swiperList: res.data || []
      })
      console.log("轮播图数据123:", this.data.swiperList)
    } catch (error) {
      console.error("轮播图接口调用失败：", error)
    }
  }
})